﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class ProductEntry : System.Web.UI.Page
    {
        private ProductManager aManager = null;
        private Product aProduct = new Product();     
        CompanyManager companyManager = new CompanyManager();     
        StoreManager storeManager = new StoreManager();
        RackManager rackManager = new RackManager();
        ShelfManager shelfManager = new ShelfManager();
        
        ProductCategoryManager productCategoryManager=new ProductCategoryManager();
        ProductGenderManager productGenderManager= new ProductGenderManager();
        ProductCategorySizeManager productCategorySizeManager=new ProductCategorySizeManager();
        ProductCategoryAgeManager productCategoryAgeManager= new ProductCategoryAgeManager();
        CountryListManager countryListManager= new CountryListManager();
        SeasonListManager seasonListManager=new SeasonListManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    LoadAllDDL();

                    string dateAsString = DateTime.Now.ToString("yyyy-MM-dd");
                    createDateTextBox.Value = dateAsString;
                }
                catch (Exception)
                {
                    
                }
            }
        }

        private void LoadAllDDL()
        {
            countrySelect.DataSource = countryListManager.GetAllCountryList();
            countrySelect.DataTextField = "countryName";
            countrySelect.DataValueField = "countryId";
            countrySelect.DataBind();
            countrySelect.Items.Insert(0, "--Select--");

            productSeasonSelect.DataSource = seasonListManager.GetAllSeasonList();
            productSeasonSelect.DataTextField = "seasonName";
            productSeasonSelect.DataValueField = "seasonId";
            productSeasonSelect.DataBind();
            productSeasonSelect.Items.Insert(0, "--Select--");

            genderSelect.DataSource = productGenderManager.GetAllProductGender();
            genderSelect.DataTextField = "GenderName";
            genderSelect.DataValueField = "GenderId";
            genderSelect.DataBind();
            genderSelect.Items.Insert(0, "--Select--");

            categoryNameSelect.DataSource = productCategoryManager.GetAllProductCategory();
            categoryNameSelect.DataTextField = "CategoryName";
            categoryNameSelect.DataValueField = "CategoryId";
            categoryNameSelect.DataBind();
            categoryNameSelect.Items.Insert(0, "--Select--");

            ageGroupSelect.DataSource = productCategoryAgeManager.GetAllProductCategoryAge();
            ageGroupSelect.DataTextField = "CategoryAgeGroup";
            ageGroupSelect.DataValueField = "CategoryAgeId";
            ageGroupSelect.DataBind();
            ageGroupSelect.Items.Insert(0, "--Select--");

            categorySizeSelect.DataSource = productCategorySizeManager.GetAllProductCategorySize();
            categorySizeSelect.DataTextField = "CategorySizeName";
            categorySizeSelect.DataValueField = "CategorySizeId";
            categorySizeSelect.DataBind();
            categorySizeSelect.Items.Insert(0, "--Select--");

            //storeSelect.DataSource = storeManager.GetAllStore();
            //storeSelect.DataTextField = "StoreName";
            //storeSelect.DataValueField = "StoreId";
            //storeSelect.DataBind();
            //storeSelect.Items.Insert(0, "--Select--");

            //rackSelect.DataSource = rackManager.GetAllRack();
            //rackSelect.DataTextField = "RackName";
            //rackSelect.DataValueField = "RackId";
            //rackSelect.DataBind();
            //rackSelect.Items.Insert(0, "--Select--");

            //shelfSelect.DataSource = shelfManager.GetAllShelf();
            //shelfSelect.DataTextField = "ShelfName";
            //shelfSelect.DataValueField = "ShelfId";
            //shelfSelect.DataBind();
            //shelfSelect.Items.Insert(0, "--Select--");
        }

        public void SaveProduct(object sender, EventArgs e)
        {
            //Generic aGeneric = new Generic();
            //Therapeutic aTherapeutic = new Therapeutic();
            //Form aForm = new Form();
            //Group aGroup = new Group();
            Company aCompany = new Company();
            //Units aUnit = new Units();
            Store aStore = new Store();
            Rack aRack = new Rack();
            Shelf aShelf = new Shelf();

            try
            {
                aManager = new ProductManager();
                aProduct.ProductBrandName = productNameTxtBox.Value;
                aProduct.TradePrice = Convert.ToDecimal(tradePriceTxtBox.Value);
                aProduct.FlatPrice = Convert.ToDecimal(flatPriceTxtBox.Value);
                aProduct.Vat = Convert.ToDecimal(vatTxtBox.Value);
                aProduct.Mrp = Convert.ToDecimal(mrpTxtBox.Value);
                aProduct.DiscountPer = Convert.ToDecimal(discountPerTxtBox.Value);
                //aProduct.PackSize = Convert.ToDecimal(packSizeTxtBox.Value);
                //aProduct.ReOrderQty = Convert.ToInt32(reOrderQtyTxtBox.Value);
                //aGeneric.GenericId = Convert.ToInt32(genericSelect.Value);
                //aProduct.AGeneric = aGeneric;
                //aTherapeutic.TherapeuticId = Convert.ToInt32(therapeuticSelect.Value);
                //aProduct.ATherapeutic = aTherapeutic;
                //aForm.FormId = Convert.ToInt32(formSelect.Value);
                //aProduct.AForm = aForm;
                //aGroup.GroupId = Convert.ToInt32(groupSelect.Value);
                //aProduct.AGroup = aGroup;
                //aCompany.CompanyId = Convert.ToInt32(companySelect.Value);
                //aProduct.ACompany = aCompany;
                //aUnit.UnitId = Convert.ToInt32(unitSelect.Value);
                //aProduct.AUnits = aUnit;
                //aStore.StoreId = Convert.ToInt32(storeSelect.Value);
                //aProduct.AStore = aStore;
                //aRack.RackId = Convert.ToInt32(rackSelect.Value);
                //aProduct.ARack = aRack;
                //aShelf.ShelfId = Convert.ToInt32(shelfSelect.Value);
                //aProduct.AShelf = aShelf;
                //aProduct.PaymentMode = Convert.ToInt32(paymentModeSelect.Value);
                //aProduct.CreateBy = createByTextBox.Value;
                aProduct.CreateDate = createDateTextBox.Value;

                string message = aManager.SaveProduct(aProduct);
                messageResult.InnerText = message;
                LoadAllDDL();
            }
            catch (Exception exception)
            {
                messageResult.InnerText = "Product information can not be saved" + "\n" + exception.ToString();
            }
        }

        public void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
        public void ClearButton(object sender, EventArgs e)
        {
            Clear();
            LoadAllDDL();
        }
        private void Clear()
        {
            productIdTxtBox.Value = "";
            productNameTxtBox.Value = "";
            tradePriceTxtBox.Value = "";
            flatPriceTxtBox.Value = "";
            vatTxtBox.Value = "";
            mrpTxtBox.Value = "";
            discountPerTxtBox.Value = "";
            packSizeTxtBox.Value = "";
            reOrderQtyTxtBox.Value = "";
            //genericSelect.Value = "";
            //therapeuticSelect.Value = "";
            //formSelect.Value = "";
            //groupSelect.Value = "";
            //companySelect.Value = "";
            //unitSelect.Value = "";
            //storeSelect.Value = "";
            //rackSelect.Value = "";
            //shelfSelect.Value = "";
            //paymentModeSelect.Value = "";
           // createByTextBox.Value = "";
            createDateTextBox.Value = "";
        }

        public void DeleteButton(object sender, EventArgs e)
        {
            try
            {
                int ProductId = Convert.ToInt32(productIdTxtBox.Value);
                ProductManager productManager = new ProductManager();
                string aa = productManager.DeleteProduct(ProductId);
                messageResult.InnerText = aa;
            }
            catch (Exception)
            {
                messageResult.InnerText = "Delete Failed";
            }
        }


    }
}